﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Navigant.Infrastructure.Cases;
using Navigant.Infrastructure.DomainBase;
using Navigant.Infrastructure.Helpers;
using Navigant.Model.Cases;
using Navigant.Model.PreservationNotices;

namespace Navigant.Web.UI
{
    public class PreservationNoticeView : LayoutsPageBase
    {
        protected PlaceHolder view;
        protected Label errorMessage;
        private int noticeId;
        
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.noticeId = 0;
            this.errorMessage.Visible = false;
            if (!string.IsNullOrEmpty(this.Request["noticeId"]))
            {
                int result;
                bool parsed = int.TryParse(this.Request["noticeId"], out result);
                if (parsed)
                {
                    this.noticeId = result;
                }
 
            }
            if (this.noticeId < 1)
            {
                this.errorMessage.Text = "Missing Preservation Notice ID.";
                this.errorMessage.Visible = true;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            ICase currentCase = null;
            try
            {
                using (SPWeb web = SPControl.GetContextSite(this.Context).OpenWeb())
                {
                    currentCase = CaseService.GetCaseBy(this.Web.Site.ID, this.Web.ID);
                    PreservationNoticeService service = new PreservationNoticeService(currentCase);
                    PreservationNotice notice = service.FindBy(this.noticeId);
                    if (notice == null)
                    {
                        this.errorMessage.Text = "Could not find the Preservation Notice specified.";
                        this.errorMessage.Visible = true;
                        return;
                    }
                    this.view.Controls.Add(this.BuildMessageTable(notice));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, currentCase);
                this.errorMessage.Text = "There was an error viewing the Preservation Notice.";
                this.errorMessage.Visible = true;
            }
        }

        private HtmlTable BuildMessageTable(PreservationNotice notice)
        {
            HtmlTable table = new HtmlTable();

            // From and Sent
            HtmlTableRow firstRow = new HtmlTableRow();
            HtmlTableCell fromLabelCell = new HtmlTableCell();
            fromLabelCell.InnerHtml = "From:&nbsp;&nbsp;";
            firstRow.Cells.Add(fromLabelCell);
            HtmlTableCell fromValueCell = new HtmlTableCell();
            fromValueCell.InnerHtml = notice.FromAddress;
            firstRow.Cells.Add(fromValueCell);
            HtmlTableCell sentLabelCell = new HtmlTableCell();
            sentLabelCell.InnerHtml = "Sent:&nbsp;&nbsp;";
            firstRow.Cells.Add(sentLabelCell);
            HtmlTableCell sentValueCell = new HtmlTableCell();
            sentValueCell.InnerHtml = notice.DateMessageSent.HasValue ? notice.DateMessageSent.Value.ToLongDateString() : string.Format("({0})", notice.Status.ToString());
            firstRow.Cells.Add(sentValueCell);
            table.Rows.Add(firstRow);

            // To
            HtmlTableRow secondRow = new HtmlTableRow();
            HtmlTableCell toLabelCell = new HtmlTableCell();
            toLabelCell.InnerHtml = "To:&nbsp;&nbsp;";
            secondRow.Cells.Add(toLabelCell);
            HtmlTableCell toValueCell = new HtmlTableCell();
            toValueCell.ColSpan = 3;
            toValueCell.InnerHtml = notice.To != null ? notice.To.Email : string.Empty;
            secondRow.Cells.Add(toValueCell);
            table.Rows.Add(secondRow);

            // Cc
            HtmlTableRow thirdRow = new HtmlTableRow();
            HtmlTableCell ccLabelCell = new HtmlTableCell();
            ccLabelCell.InnerHtml = "Cc:&nbsp;&nbsp;";
            thirdRow.Cells.Add(ccLabelCell);
            HtmlTableCell ccValueCell = new HtmlTableCell();
            ccValueCell.ColSpan = 3;
            ccValueCell.InnerHtml = string.Empty;
            thirdRow.Cells.Add(ccValueCell);
            table.Rows.Add(thirdRow);

            // Subject
            HtmlTableRow fourthRow = new HtmlTableRow();
            HtmlTableCell subjectLabelCell = new HtmlTableCell();
            subjectLabelCell.InnerHtml = "Subject:&nbsp;&nbsp;";
            fourthRow.Cells.Add(subjectLabelCell);
            HtmlTableCell subjectValueCell = new HtmlTableCell();
            subjectValueCell.ColSpan = 3;
            subjectValueCell.InnerHtml = notice.Subject;
            fourthRow.Cells.Add(subjectValueCell);
            table.Rows.Add(fourthRow);

            // Attachments
            if (notice.Attachments.Count > 0)
            {
                HtmlTableRow attachmentsRow = new HtmlTableRow();
                HtmlTableCell cell = new HtmlTableCell();
                cell.ColSpan = 4;
                HtmlTable innerTable = new HtmlTable();
                HtmlTableRow innerRow = new HtmlTableRow();
                HtmlTableCell innerCell = null;
                foreach (Attachment attachment in notice.Attachments)
                {
                    innerCell = new HtmlTableCell();
                    HtmlAnchor anchor = new HtmlAnchor();
                    anchor.HRef = string.Format("{0}/{1}", this.Web.Url, attachment.OriginalUrl);
                    anchor.InnerHtml = attachment.FileName;
                    innerCell.Controls.Add(anchor);
                    innerRow.Cells.Add(innerCell);
                }
                innerTable.Rows.Add(innerRow);
                cell.Controls.Add(innerTable);
                attachmentsRow.Cells.Add(cell);
                table.Rows.Add(attachmentsRow);
            }

            // Body
            HtmlTableRow bodyRow = new HtmlTableRow();
            HtmlTableCell bodyCell = new HtmlTableCell();
            bodyCell.ColSpan = 4;
            Panel panel = new Panel();
            panel.BorderWidth = new Unit(1);
            panel.BorderStyle = BorderStyle.Solid;
            panel.Height = new Unit(200);
            panel.Controls.Add(new LiteralControl(notice.Body));
            bodyCell.Controls.Add(panel);
            bodyRow.Cells.Add(bodyCell);
            table.Rows.Add(bodyRow);

            return table;
        }
    }
}